package persistent.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import basic.BaseDAO;
import model.TReview;
import model.VForumContent;
import model.VReview;
import persistent.dao.ReviewDAO;

public class ReviewDAOImpl implements ReviewDAO {

	private BaseDAO dao = null;
	public ReviewDAOImpl() {
		super();
		dao = new BaseDAO();
	}
	@Override
	public int addReview(TReview review) {
		String sql="up_addReview(?,?,?,?)";
		Object[] param={review.getForumid(),review.getReviewcontent(),review.getUserid(),review.getIpaddr()};
		int row=(Integer)dao.executeSelectProduce(sql, param);
		dao.close();
		return row;
	}

	@Override
	public boolean deleteReivew(int id) {
		String sql="delete from T_Review where reviewid =?";
		Object[] param={id};
		int row=dao.delete(sql, param);
		if(row>0)
			return true;
		else 
			return false;
		
	}

	@Override
	public boolean deleteReviewsBelongForum(int forumid) {
		String sql="delete from T_Review where forumid =?";
		Object[] param={forumid};
		int row=dao.delete(sql, param);
		if(row>0)
			return true;
		else 
			return false;
	}

	@Override
	public List<VReview> getReviewlistByForumId(int forumid) {
		String sql="select*from T_Review where reviewid=?";
		Object[] param={forumid};
		ResultSet rs=dao.select(sql, param);
		List<VReview> list=VReview.toList(rs);
		dao.close();
		return list;
	}

	@Override
	public VReview getReviewById(int reviewid) {
		String sql="select*from V_Review where reviewid=?";
		Object[] param={reviewid};
		ResultSet rs=dao.select(sql, param);
		try {
			rs.next();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		VReview list=new VReview(rs);
		dao.close();
		return list;
	}

}
